package com.qijia.travel.helper;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.awt.Color;
import javax.imageio.ImageIO;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class ImgHelper {

	/**
	 * 转化图片尺寸,并保留原始图片
	 * 
	 * @param imgfile
	 *            图片名
	 * @param desfile
	 *            原始图片存储名称
	 * @param customerWidth
	 *            自定义宽带
	 * @param customerHeight
	 *            自定义高度
	 */
	public static void convrtImg(String imgfile, String desfile,
			int customerWidth, int customerHeight) {
		// 图片缓存
		BufferedImage img2 = null;
		File stadimgfile = new File(imgfile);
		File _desfile = new File(desfile);
		
		try {
			if (!_desfile.exists())
				_desfile.createNewFile();
			
			img2 = ImageIO.read(stadimgfile);
			// 得到图片的宽和高
			double width = img2.getWidth();
			double height = img2.getHeight();

			double ratew = customerWidth / width;
			double rateh = customerHeight / height;

			// 获得适合的缩放比率，即以在规定缩略尺寸中完整显示图片内容的同时又保证最大的缩放比率
			double rate = Math.min(ratew, rateh);
			rate = (Math.rint((rate * 100) + 0.5)) / 100;

			BufferedImage imgmini = new java.awt.image.BufferedImage(
					customerWidth, customerHeight,
					BufferedImage.TYPE_USHORT_565_RGB);

			Graphics2D gmini = imgmini.createGraphics();
			gmini.setBackground(Color.WHITE);
			gmini.clearRect(0, 0, customerWidth, customerHeight);
			AffineTransform trans = new AffineTransform();
			trans.scale(rate, rate);

			AffineTransformOp op = new AffineTransformOp(trans,
					AffineTransformOp.TYPE_BILINEAR);
			gmini.drawImage(img2, op,
					(int) (customerWidth - (width * rate)) / 2,
					(int) (customerHeight - (height * rate)) / 2);
			ImageIO.write(imgmini, "jpg", _desfile);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 转化图片尺寸
	 * 
	 * @param imgfile
	 *            图片名
	 * @param customerWidth
	 *            自定义宽带
	 * @param customerHeight
	 *            自定义高度
	 */
	public static void convrtImg(String imgfile, int customerWidth,
			int customerHeight) {

		// 图片缓存
		BufferedImage img2 = null;
		File stadimgfile = new File(imgfile);
		try {
			img2 = ImageIO.read(stadimgfile);
			// 得到图片的宽和高
			double width = img2.getWidth();
			double height = img2.getHeight();

			double ratew = customerWidth / width;
			double rateh = customerHeight / height;

			// 获得适合的缩放比率，即以在规定缩略尺寸中完整显示图片内容的同时又保证最大的缩放比率
			double rate = Math.min(ratew, rateh);
			rate = (Math.rint((rate * 100) + 0.5)) / 100;

			BufferedImage imgmini = new java.awt.image.BufferedImage(
					customerWidth, customerHeight,
					BufferedImage.TYPE_USHORT_565_RGB);

			Graphics2D gmini = imgmini.createGraphics();
			gmini.setBackground(Color.WHITE);
			gmini.clearRect(0, 0, customerWidth, customerHeight);
			AffineTransform trans = new AffineTransform();
			trans.scale(rate, rate);

			AffineTransformOp op = new AffineTransformOp(trans,
					AffineTransformOp.TYPE_BILINEAR);
			gmini.drawImage(img2, op,
					(int) (customerWidth - (width * rate)) / 2,
					(int) (customerHeight - (height * rate)) / 2);
			ImageIO.write(imgmini, "jpg", stadimgfile);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 加水印
	 * 
	 * @param pressImg --
	 *            用作水印的图片
	 * @param targetImg --
	 *            把水印图片附加到的目标图片
	 * @param x --
	 *            距离图片右下角的左方向偏移量
	 * @param y --
	 *            距离图片右下角的上方向偏移量
	 */
	public static void addSignCode(String pressImg, String targetImg, int x,
			int y) {
		try {
			File _file = new File(targetImg);
			File _filebiao = new File(pressImg);
			if (!_file.exists() || !_filebiao.exists())
				return;

			Image src = ImageIO.read(_file);
			int wideth = src.getWidth(null);
			int height = src.getHeight(null);

			BufferedImage image = new BufferedImage(wideth, height,
					BufferedImage.TYPE_INT_RGB);
			Graphics g = image.createGraphics();
			g.drawImage(src, 0, 0, wideth, height, null);

			// 水印文件
			Image src_biao = ImageIO.read(_filebiao);

			int wideth_biao = src_biao.getWidth(null);
			int height_biao = src_biao.getHeight(null);

			g.drawImage(src_biao, wideth - wideth_biao - x, height
					- height_biao - y, wideth_biao, height_biao, null);
			g.dispose();

			FileOutputStream out = new FileOutputStream(targetImg);
			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
			encoder.encode(image);

			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 使用FileInputStream和FileOuStream
	public static void copyfile(String pathSrc, String pathDest) {
		FileInputStream fi = null;
		FileOutputStream fo = null;
		try {
			fi = new FileInputStream(pathSrc);
			fo = new FileOutputStream(pathDest);

			byte data[] = new byte[fi.available()];
			fi.read(data);
			fo.write(data);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				fi.close();
				fo.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static void main(String[] args){
		ImgHelper.convrtImg("d:/a.jpg", "d:/b.jpg", 500, 500);
	}
}
